package com.tanzeer.deliverytracker.fragments;

import java.util.ArrayList;

import com.tanzeer.deliverytracker.DetalisDeliveryActivity;
import com.tanzeer.deliverytracker.R;
import com.tanzeer.deliverytracker.adapter.ActivesAdapter;
import com.tanzeer.deliverytracker.dbhelper.DBOpenHelper;
import com.tanzeer.deliverytracker.models.DelivaryTracks;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Fragment_Actives extends Fragment {

	private ListView listView;
	ActivesAdapter adapter;
	ArrayList<DelivaryTracks> list;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_actives, null, false);
		listView = (ListView) view.findViewById(R.id.listViewactives);
		String type = getArguments().getString("type");

		DBOpenHelper dbheHelper = new DBOpenHelper(getActivity());
		if (type.equals("Active")) {
			list = dbheHelper.getAllList();

		} else if (type.equals("Completed")) {
			list = dbheHelper.getAllCompleteList();
		} else {
			list = dbheHelper.getActiveCategoryListByType(type);
		}

		Log.e("msg", "" + list.size());
		adapter = new ActivesAdapter(getActivity(),
				R.layout.delivery_tracks_list_row, list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Intent intent = new Intent(getActivity(),
						DetalisDeliveryActivity.class);

				DelivaryTracks object = list.get(position);
				intent.putExtra("track_number", object.getId());
				intent.putExtra("description", object.getDescription());
				intent.putExtra("type", object.getType());
				intent.putExtra("id", object.getRowId());
				startActivity(intent);

			}
		});
		return view;
	}

}
